package cc.z8g.browser.core.tab

import cc.z8g.browser.R
import cc.z8g.browser.utils.ThemeUtils
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.TransitionDrawable
import androidx.core.content.ContextCompat

/** 创建一个具有指定图层列表的新过渡可绘制对象。至少需要2个图层才能使此可绘制对象正常工作 */
internal class BackgroundDrawable(context: Context) : TransitionDrawable(
    arrayOf<Drawable>(
        ColorDrawable(ContextCompat.getColor(context, R.color.transparent)),
        ColorDrawable(ThemeUtils.getColor(context, R.attr.selectedBackground))
    )
) {

    private var isSelected: Boolean = false

    override fun startTransition(durationMillis: Int) {
        if (!isSelected) {
            super.startTransition(durationMillis)
        }
        isSelected = true
    }

    override fun reverseTransition(duration: Int) {
        if (isSelected) {
            super.reverseTransition(duration)
        }
        isSelected = false
    }

}
